
local api = ac.frame

--添加一个拖动
function api:add_drag_button(x,y,w,h)
    local button = self:add_button('',x,y,w,h)
    local ui = self
    button.name = '拖动按钮'
    button.drag_flag = true

    function button:on_button_mouse_down()
        local x,y = ac.hardware:get_mouse_pos()
        self.start_x = x
        self.start_y = y
    end

    function button:on_button_drag(x,y)
        ui:set_position(ui.x + x - self.start_x,ui.y + y - self.start_y)
        self.start_x = x
        self.start_y = y
    end
    
    function button:on_button_drag_end()
        print(ui:get_real_position())
    end

    return button
end




--添加一个边框面板
function api:add_frame(frame,...)
    local params = {...}
    local x,y,w,h = table.unpack(params)
    if #params==1 then
        local size = params[1]
        w = self.w * size
        h = self.h * size
        x = (self.w - w)/2
        y = (self.h - h)/2
    end
    local tool = class.panel:builder
    {
        parent = self,
        fdf = frame,
        x = x,
        y = y,
        w = w,
        h = h,
    }
    return tool
end

--添加一个数量下标
function api:add_number_panel(...)
    local params = {...}
    local x,y,w,h,font_size = table.unpack(params)
    w = w or 36
    h = h or 32
    if #params==1 then
        local size = params[1]
        w = 36 * size
        h = 32 * size
    end
    x = x or self.w - w
    y = y or self.h - h
    local tool = class.panel:builder
    {
        parent = self,
        type = 'panel',
        normal_image = [[UI\Widgets\Console\Human\CommandButton\human-button-lvls-overlay.blp]],
        x = x,
        y = y,
        w = w,
        h = h,
        text = {
            type = 'text',
            text = '',
            align = 5,
            justH = 'CENTER',
            justV = 'MIDDLE',
            outline = true,
            font_size = font_size or 10,
        }
    }
    return tool
end


--添加一个带名字的按钮
function api:add_namebutton(name,x,y,w,h,size,align,frame)
    local button = self:add_button('',x,y,w,h,1)
    local frame = frame or 'tooltip_backdrop0'
    button.name = name
    button:add_frame(frame)
    button.hover_image = ''
    button.text = button:add_text(name,0,0,nil,nil,size or 12,5)
    button:set_highlight([[UI\Widgets\BattleNet\bnet-button01-highlight-mouse.blp]])
    return button
end



--[翻页区域]
function api:add_page_area(x,y,w,h)
    local w0,h0 = 16,14 --翻页按钮大小
    local w1 = 80   --页码显示大小
    local x = x or (self.w - 2*w0 - w1)/2
    local y = y or (self.h - 45)
    local w = (2*w0 + w1)
    local h = h0
    local panel = self:add_panel('',x,y,w,h)
    local art1 = [[ui\上一页按钮.tga]]
    local art2 = [[ui\下一页按钮.tga]]
    local page_los = panel:add_button(art1,0,0,w0,h0)
    local page_add = panel:add_button(art2,w0+w1,0,w0,h0)
    -- page_los:set_pushedd(art1_2)
    -- page_add:set_pushedd(art2_2)
    -- page_los:set_highlight(art1_1)
    -- page_add:set_highlight(art2_1)

    self.page_text = panel:add_text('1/1',w0,0,w1,h0,10,5)
    self.page = 1
    self.max_page = 1
    self.page_los = page_los
    self.page_add = page_add
    return panel
end



function api:play_animation()
    if self.animation_icon==nil then 
        return
    end
    if self._timer then 
        self._timer:remove()
        self._timer = nil
    end 
    local num = 0
    local time = self.animation_frame or 30
    self._timer = ac.loop(time,function (timer)
        num = num + 1
        if (num > self.animation_count) or self.handle == nil or self.handle == 0 then 
            self.animation_icon:set_normal_image('')
            timer:remove()
            return
        end 
        local str = string.format(self.animation_path,num)
        self.animation_icon:set_normal_image(str)
    end)
end

